package com.cloud.wms.ui.in.inventory;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.core.utils.TimeUtils;
import com.cloud.widget.ScanText;
import com.cloud.widget.expand.ExpandLayout;
import com.cloud.wms.R;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.in.contract.InInventoryContract;
import com.cloud.wms.ui.in.entity.InCommodityEntity;
import com.cloud.wms.ui.in.presnter.InInventoryPresenter;
import com.cloud.wms.ui.main.entity.BatchEntity;
import com.cloud.wms.ui.main.entity.MemberEntity;
import com.cloud.wms.ui.main.entity.TaskEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-23 19:37
 * @Description: 无单--逐件清点
 */
public class NoSingleActivity extends BaseBarListActivity<InCommodityEntity, InInventoryPresenter> implements InInventoryContract.View {
    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.btn_good)
    CheckedTextView btnGood;
    @BindView(R.id.btn_bad)
    CheckedTextView btnBad;
    @BindView(R.id.tv_member_name)
    TextView tvMemberName;
    @BindView(R.id.iv_member_name)
    ImageView ivMemberName;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.iv_switch_batch)
    CheckedTextView tvSwitchBatch;
    private InCommodityEntity commodityEntity;
    private String beginTime;

    @Override
    protected String barTitle() {
        return "逐件清点";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_in_inventory_no_single;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_common_commodity;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MemberEntity memberInfo = FineExApplication.component().loginCache().getMemberInfo();
        if (tvMemberName != null && memberInfo != null) {
            tvMemberName.setText(memberInfo.getMemberName());
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        beginTime = TimeUtils.getCurrentDateAndTime();
        btnSubmit.setEnabled(false);
        initScanText(scanText);
        mAdapter.setNewInstance(new ArrayList<InCommodityEntity>());
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        if (!tvSwitchBatch.isChecked()) {
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                InCommodityEntity entity = mAdapter.getData().get(i);
                if (entity.matchBarCode(result)) {
                    entity.setAmount(entity.getAmount() + 1);
                    mAdapter.notifyDataSetChanged();
                    return;
                }
            }
        }
        mPresenter.checkCommodityCode(null, result);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, InCommodityEntity item) {
        helper.setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_commodity_code, item.getBarCode())
                .setText(R.id.tv_commodity_type, item.getStockTypeName())
                .setText(R.id.tv_commodity_amount, item.getAmount() + "")
                .setText(R.id.tv_produce_date, item.getProductionDate())
                .setText(R.id.tv_deadline_date, item.getExpirationDate())
                .setText(R.id.tv_in_date, item.getInDate());

        ImageView ivArrow = helper.getView(R.id.iv_arrow);
        ExpandLayout expand = helper.getView(R.id.expand_layout);
        expand.setOnExpansionUpdateListener((expansionFraction, state) -> ivArrow.setRotation(expansionFraction * 180));
        helper.getView(R.id.ll_title_content).setOnClickListener(v -> expand.toggle(true));
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case InInventoryPresenter.IN_CHECK_CODE:
                commodityEntity = (InCommodityEntity) message.obj;
                tvMemberName.setText(commodityEntity.getMemberName());
                commodityEntity.setStockType(btnBad.isChecked() ? 2 : 1);
                SwitchBatchActivity.start(this);
                break;
            case InInventoryPresenter.IN_SINGLE_SUBMIT:
                success("清点提交成功!");
                this.finish();
                break;
        }
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.SWITCH_BATCH:
                if (commodityEntity.isBatch()) {
                    tvSwitchBatch.setChecked(false);
                    BatchEntity batchEntity = (BatchEntity) event.getData();
                    commodityEntity.setProductionDate(batchEntity.getProductionDate());
                    commodityEntity.setInDate(batchEntity.getInDate());
                    commodityEntity.setExpirationDate(batchEntity.getExpirationDate());
                    commodityEntity.setAmount(commodityEntity.getAmount() + 1);
                    mAdapter.addData(0, commodityEntity);
                }
                tvSwitchBatch.setChecked(false);
                btnSubmit.setEnabled(true);
                break;
        }
    }

    @OnClick({R.id.btn_good, R.id.iv_switch_batch, R.id.btn_bad, R.id.tv_member_name, R.id.iv_member_name, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_good:
                btnGood.setChecked(true);
                btnBad.setChecked(false);
                switchCommodityType();
                break;
            case R.id.btn_bad:
                btnGood.setChecked(false);
                btnBad.setChecked(true);
                switchCommodityType();
                break;
            case R.id.iv_switch_batch:
                tvSwitchBatch.setChecked(!tvSwitchBatch.isChecked());
                break;
            case R.id.tv_member_name:
            case R.id.iv_member_name:
                switchMember();
                break;
            case R.id.btn_submit:
                mPresenter.submitSingleNoOrderInventory(mAdapter.getData(), beginTime);
                break;
        }
    }

    private void switchCommodityType() {
        if (commodityEntity != null) {
            int commodityType = 1;
            if (btnBad.isChecked()) commodityType = 2;
            commodityEntity.setStockType(commodityType);
        }
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, NoSingleActivity.class);
        context.startActivity(intent);
    }
}
